function [ EC ] = APEsingleALS(lambda, p, itter)
% [ EC ] = APEsingleALS(lambda, p, itter)
%
% Script to convert single .sif file into APE-.mat format as well as to 
% CSV or Excel
%
% INPUT     lambda      :   Lambda paramter of ALS routine (def= 10000)
%  (opt.)   p           :   p paramter of ALS routine (def= 0.01)
%           itter       :   Itterations (def= 10)
%
% OUTPUT    EC          :   error code (0 = success, 1 = error)
%

%  (c) F. Dworkowski, 2011
%      Version 1.0, 2011-08-03


%% Config
% savetypepreset = 0;                % Config save type
logfilename = ['log-APEsingleALS_',date,'.txt'];
EC = 0;
oldpath=cd;
if nargin < 3
    disp('Standard values for lambda, p, itter were chosen')
    lambda  = 10000;
    p       = 0.01;
    itter   = 10;
end
    
      
%% Choose Folder and detect files
%PathName = uigetdir(oldpath,'Choose Folder to Batch Convert') 
[FileName,PathName,FilterIndex] = uigetfile({'*.mat';'*.*'});
if isequal(PathName,0)
   disp('User selected Cancel')
   EC = 1;
   return
else
   disp(['User selected ', fullfile(PathName, FileName)])
end

cd(PathName)

% Initialize Logfile
logfilename = fullfile(PathName,logfilename);
diary(logfilename)
disp(' ')
disp('===========    SLS APE .sif Converter    ===========')
disp(['===========     ',datestr(now),'     ==========='])
disp(' ')

%% Conversion 
    [pathstr, datasetname, ext] = fileparts(FileName);
    datasave=load(datasetname);
    datasetsize=length(datasave.datay(1,:));
            h = waitbar(0,'Please wait...',...
            'Name',['ALS correction of ', datasetname]);
    if datasetsize>1
        for j = 1:datasetsize
            waitbar((j/datasetsize)*0.9,h,['Spectrum ',num2str(j),'/',num2str(datasetsize)])
            if mean(datasave.datay(:,j)) ~= 0
                [datasave.ALSbasey(:,j), datasave.ALSres(:,j)] = ALSbaseline(datasave.datay(:,j), lambda, p, itter);
                datasave.ALSdatay(:,j) = datasave.datay(:,j) - datasave.ALSbasey(:,j);    
            else
                errordlg({'ERROR:' 'One Spectrum in' ['    ',datasetname] ...
                    'seams to be empty!!' ...
                    ['Last converted Spectrum: ',num2str(j-1),'/',num2str(datasetsize)]})
                break
            end
        end
    else
        [datasave.ALSbasey, datasave.ALSres] = ALSbaseline(datasave.datay(:,1), lambda, p, itter);
        datasave.ALSdatay   = datasave.datay(:,1) - datasave.ALSbasey;
    end
    datasave.ALSpara = cell(3,2);
    datasave.ALSpara(1,1)={'lambda'}; datasave.ALSpara(1,2)={lambda};
    datasave.ALSpara(2,1)={'p'}; datasave.ALSpara(2,2)={p};
    datasave.ALSpara(3,1)={'itter'}; datasave.ALSpara(3,2)={itter};   
waitbar(0.9,h,[datasetname,' converted.']);
%% Classical Save
    save(datasetname, '-struct', 'datasave')

    disp(['File ',datasetname,'.mat saved.'])      % LOG output
waitbar(1,h,[datasetname,' saved.']);
pause(2)
close(h)    

cd(oldpath);            % go back to old path
diary off;              % suspend logging
EC = 0;
end

